//
//  ICBaseVC.swift
//  Dawn
//
//  Created by cuicheng on 2021/9/2.
//

import UIKit

typealias VoidClosure = (()->())
typealias IndexClosure = ((Int)->())
protocol ICBaseVCProtocol {
    func _initUI()
    func _initData()
    func _initFrame()
    func _handleEvent()
}

class ICBaseVC: UIViewController, ICBaseVCProtocol {
    
    public var args: JSON?
    lazy var navBar = DawnNavView()
    private var backBtn: UIButton?
    var canPanBack = true {
        didSet{
            navigationController?.interactivePopGestureRecognizer?.delegate = canPanBack ? nil : self
        }
    }
    var canNavBack = false
    {
        didSet{
            if canNavBack {
                if backBtn != nil { backBtn?.isHidden = false; return }
                backBtn = navBar.addLeft(title: "\u{e650}", width: 50.adapter) {[weak self] in
                    self?.navigationController?.popViewController(animated: true)
                }
            }else {
                if backBtn != nil { backBtn?.isHidden = true }
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        canPanBack = canPanBack == true
        navigationController?.navigationBar.isHidden = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .background
        _initData()
        _initUI()
        _initFrame()
    }
    

    func _initUI() {
        
        view.addSubview(navBar)
        canNavBack = navigationController?.viewControllers.first != self
    }
    
    func _initData() {
        
    }
    
    func _initFrame() {
        navBar.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: UIScreen.navH)
    }
    
    func _handleEvent() {
        
    }
    
    func _reloadData(completion: VoidClosure? = nil) {
        DispatchQueue.main.asyncAfter(deadline: .now()+2) {
            completion?()
        }
    }
    
    deinit {
        LogUtil.log("\(classForCoder) 释放了")
    }

}

extension ICBaseVC: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return false
    }
}
